
$(function () {
    let parmes = {
        key: '',
        type: '',
        state: '',
        page: 1,
        perpage: 10
    }


    function articleQuery() {
        $.get('/admin/article/query', parmes, function (res) {
            const html = template('trTpl', { list: res.data.data })
            $('tbody').html(html)
            renderPager(res.data.totalCount)
        })
    }

    // 数据渲染到下拉框中
    function getCategoryList() {
        $.get('/admin/category/list', function (res) {
            let html = `<option value=''>所有分类</option>`
            res.data.forEach(value => {
                html += `<option value="${value.id}">${value.name}</option>`
                $('#selCategory').html(html)
            });
        })
    }
    // 分页数据渲染
    function renderPager(count) {
        layui.laypage.render({
            elem: 'pager',//注意，这里的ID，不用加 # 号
            count,
            limit: parmes.perpage,
            curr: parmes.page,
            jump(obj, first) {
                if (!first) {
                    parmes.page = obj.curr;
                    articleQuery();
                }
            }
        });
    }

    // 实现筛选功能
    function filtrate() {
        $('#btnSearch').click(function () {
            parmes.key = $('#aname').val()
            parmes.type = $('#selCategory').val()
            parmes.state = $('#selStatus').val()
            parmes.page = 1
            articleQuery();
        })
    }


    // 重置按钮
    $('#btnReset').click(function () {
        parmes = { key: '', type: '', state: '', page: 1, perpage: 10 }
        $('#aname').val('')
        $('#selCategory').val('')
        $('#selStatus').val('')
        articleQuery();
    })
    articleQuery()
    getCategoryList()
    filtrate()
})